package com.yuanda.wordhot.service;

import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mchange.v2.c3p0.PooledDataSource;
import com.yuanda.wordhot.entity.DataSourceStateEntity;

@Service
public class DataSourceMonitService {
	@Autowired
	private PooledDataSource dataSource;

	public int getNumConnectionsDefaultUser() throws SQLException {
		return dataSource.getNumConnectionsDefaultUser();
	}

	public int getNumIdleConnectionsDefaultUser() throws SQLException {
		return dataSource.getNumIdleConnectionsDefaultUser();
	}
	
	public int getNumBusyConnectionsDefaultUser() throws SQLException {
		return dataSource.getNumBusyConnectionsDefaultUser();
	}

	public int getNumUnclosedOrphanedConnectionsDefaultUser()
			throws SQLException {
		return dataSource.getNumUnclosedOrphanedConnectionsDefaultUser();
	}

	public DataSourceStateEntity getDataSourceState() throws SQLException {
		DataSourceStateEntity state = new DataSourceStateEntity();
		state.setNumBusyConnectionsDefaultUser(this
				.getNumBusyConnectionsDefaultUser());
		state.setNumConnectionsDefaultUser(this.getNumConnectionsDefaultUser());
		state.setNumIdleConnectionsDefaultUser(this
				.getNumIdleConnectionsDefaultUser());
		state.setNumUnclosedOrphanedConnectionsDefaultUser(this
				.getNumUnclosedOrphanedConnectionsDefaultUser());
		return state;
	}
}
